package com.example.xianji.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.adapter.Replay_Adapter;
import com.example.xianji.adapter.Share_Adapter;
import com.example.xianji.bean.Replay;


import com.example.xianji.constant.Constant;
import com.example.xianji.utils.SharesUtil;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.example.xianji.widgets.PullToRefreshLayout_two;
import com.example.xianji.widgets.PullToRefreshLayout_two.OnRefreshListener;
import com.example.xianji.widgets.X5WebView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.sina.weibo.sdk.api.BaseMediaObject;
import com.sina.weibo.sdk.api.WebpageObject;
import com.sina.weibo.sdk.api.WeiboMessage;
import com.sina.weibo.sdk.api.WeiboMultiMessage;
import com.sina.weibo.sdk.api.share.BaseResponse;
import com.sina.weibo.sdk.api.share.IWeiboHandler;
import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
import com.sina.weibo.sdk.api.share.SendMessageToWeiboRequest;
import com.sina.weibo.sdk.api.share.SendMultiMessageToWeiboRequest;
import com.sina.weibo.sdk.api.share.WeiboShareSDK;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.auth.sso.SsoHandler;
import com.sina.weibo.sdk.constant.WBConstants;
import com.sina.weibo.sdk.exception.WeiboException;
import com.sina.weibo.sdk.utils.Utility;
import com.tencent.connect.common.Constants;
import com.tencent.connect.share.QQShare;
import com.tencent.smtt.sdk.CookieSyncManager;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.text.ClipboardManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.open.t.Weibo;

/**
 * 服务详情页面
 *
 * @author me
 */
public class Main_FuWu_DetailsActivity extends Activity implements
        OnClickListener, OnRefreshListener, IWeiboHandler.Response {
    /*************************** 服务详情页面 ******************************/
    private ImageView iamge_collection, imageview_share,
            fuwu_detail_image_load;
    private RelativeLayout xuqiu_detail_back;
    private TextView fuwu_details_title, fuwu_details_gongsi_name,
            fuwu_details_tag, fuwu_details_gongsi_phone,
            fuwu_details_is_renzheng;
    private X5WebView fuwu_details_webview;
    private PullToRefreshLayout_two fuwu_details_PullToRefreshLayout_two;
    private Button fuwu_details_liuyan, button_pinglun, send_pinglun_contant;
    private LinearLayout linearlayout_pinglun, linearlayout_details, layout2;
    private int fuwu_id;
    //    private SharedPreferences sp;
//    private Editor ed;
    private ClipboardManager clipboard;
    private InputMethodManager manager;
    private EditText editText_pinglun_contant;
    private ArrayList<Replay> replay_list = new ArrayList<Replay>();

    private Replay_Adapter mReplay_Adapter;

    private int page = 1;
    private int user_id;
    private String phone;
    private GridView province_gridview;
    private PopupWindow show_popupWindow;

    private Share_Adapter mShare_Adapter;

    private SsoHandler mSsoHandler;
    private IWeiboShareAPI mWeiboShareAPI = null;
    private Oauth2AccessToken mAccessToken;
    private boolean isInstalledWeibo;
    private int supportApiLevel;
    private String share_url;
    private Message message;
//	private UserAPI userAPI; // 用户模块处理对象
//	private OAuthV1 oAuth;
//	private TAPI tapi;

    private String response; // 返回信息
    private Weibo mWeibo = null;
    /**
     * 腾讯微博
     */
    IUiListener loginListener = new BaseUiListener() {
        @Override
        protected void doComplete(JSONObject values) {
            initOpenidAndToken(values);
            mWeibo = new Weibo(Main_FuWu_DetailsActivity.this, XJApplication
                    .getTencent().getQQToken());
            if (ready(Main_FuWu_DetailsActivity.this)) {
                String content = fuwu_details_title.getText().toString();
                mWeibo.sendText(content, new TQQApiListener("add_t", false,
                        Main_FuWu_DetailsActivity.this));
            }
        }
    };
    private String mLastAddTweetId = null;
    private AnimationDrawable fuwu_detail_animation;
    private String pic[];
    private int visible;
    private int booth_id;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 2) {
                fuwu_details_PullToRefreshLayout_two.refreshFinish(PullToRefreshLayout_two.SUCCEED);
            }
            if (msg.what == 3) {
                mReplay_Adapter.notifyDataSetChanged();
                fuwu_details_PullToRefreshLayout_two.loadmoreFinish(PullToRefreshLayout_two.SUCCEED);
            }
            if (msg.what == 4) {
                fuwu_details_gongsi_phone.setText("联系方式:" + "  " + phone);
            }
            if (msg.what == 5) {
                get_fuwu_datils();
            }
            if (msg.what == 0) {
                Toast.makeText(Main_FuWu_DetailsActivity.this, "发送成功失败", Toast.LENGTH_LONG).show();
            }
            if (msg.what == 6) {
                Toast.makeText(Main_FuWu_DetailsActivity.this, "发送成功", Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__fu_wu__details);
        // 初始化
        init();

        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                get_fuwu_datils();
            }
        });

        xuqiu_detail_back.setOnClickListener(this);
        fuwu_details_liuyan.setOnClickListener(this);
        button_pinglun.setOnClickListener(this);
        linearlayout_pinglun.setOnClickListener(this);
        fuwu_details_PullToRefreshLayout_two.setOnRefreshListener(this);
        send_pinglun_contant.setOnClickListener(this);
        linearlayout_details.setOnClickListener(this);
        iamge_collection.setOnClickListener(this);

        imageview_share.setOnClickListener(this);
        /************ WebView 触摸事件 ***************************/
        touchEvent();
    }

    private void init() {
        xuqiu_detail_back = (RelativeLayout) findViewById(R.id.xuqiu_detail_relative_back);
        fuwu_details_title = (TextView) findViewById(R.id.fuwu_details_title);
        fuwu_details_gongsi_name = (TextView) findViewById(R.id.fuwu_details_gongsi_name);
        fuwu_details_tag = (TextView) findViewById(R.id.fuwu_details_tag);
        fuwu_details_webview = (X5WebView) findViewById(R.id.fuwu_details_webview);
        fuwu_details_gongsi_phone = (TextView) findViewById(R.id.fuwu_details_gongsi_phone);
        fuwu_details_is_renzheng = (TextView) findViewById(R.id.fuwu_details_is_renzheng);
        fuwu_details_PullToRefreshLayout_two = (PullToRefreshLayout_two) findViewById(R.id.fuwu_details_PullToRefreshLayout_two);

        fuwu_details_liuyan = (Button) findViewById(R.id.fuwu_details_liuyan);
        linearlayout_pinglun = (LinearLayout) findViewById(R.id.linearlayout_pinglun);
        button_pinglun = (Button) findViewById(R.id.button_pinglun);
        linearlayout_details = (LinearLayout) findViewById(R.id.linearlayout_details);
        imageview_share = (ImageView) findViewById(R.id.fuwu_imageview_share);
        fuwu_detail_image_load = (ImageView) findViewById(R.id.fuwu_detail_image_load);

        iamge_collection = (ImageView) findViewById(R.id.iamge_collection);
        fuwu_detail_animation = (AnimationDrawable) fuwu_detail_image_load
                .getDrawable();

        fuwu_detail_animation.start();
        share_url = "http://m.xianjichina.com/service/detail_"
                + getIntent().getIntExtra("fuwu_details_id", 0) + ".html";
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        editText_pinglun_contant = (EditText) findViewById(R.id.editText_pinglun_contant);
        send_pinglun_contant = (Button) findViewById(R.id.send_pinglun_contant);

        mReplay_Adapter = new Replay_Adapter(replay_list, this);
        clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        CookieSyncManager.createInstance(this);
        CookieSyncManager.getInstance().sync();

        /*******************防止有黑色屏幕出现，使窗口支持透明度******************************/
        getWindow().setFormat(PixelFormat.TRANSLUCENT);

        //fuwu_details_webview.setBackgroundColor(Color.TRANSPARENT); // WebView 背景透明效果
    }

    private void touchEvent() {
        fuwu_details_webview.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    visible = linearlayout_pinglun.getVisibility();
                    if (visible == View.VISIBLE) {
                        linearlayout_pinglun.setVisibility(View.GONE);
                        hideKeyboard();
                        return true;
                    }
                }
                return false;
            }
        });

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.xuqiu_detail_relative_back) {
            this.finish();

        } else if (v.getId() == R.id.fuwu_details_liuyan) {
            if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
//            if (sp.getBoolean("xianji_denglu", false)) {
                Intent intent = new Intent(Main_FuWu_DetailsActivity.this,
                        Main_FuWu_LiuYanActivity.class);

                intent.putExtra("liuyan_comefrom", 1);
                intent.putExtra("fuwu_id", fuwu_id);
                startActivity(intent);
            } else {
                Toast.makeText(Main_FuWu_DetailsActivity.this, "请先登录", Toast.LENGTH_SHORT)
                        .show();
                Contant.come_from_fuwuDetail = 1;// 从服务详情页面到登录页面
                Intent intent = new Intent(Main_FuWu_DetailsActivity.this,
                        Main_LoginActivity.class);
                intent.putExtra("origin_login", 52);

                startActivity(intent);
            }

        } else if (v.getId() == R.id.button_pinglun) {
            Intent intent = new Intent(Main_FuWu_DetailsActivity.this,
                    Main_QiYe_DetailsActivity.class);

            intent.putExtra("gongsi_id", booth_id);

            startActivity(intent);

        } else if (v.getId() == R.id.linearlayout_details) {
            linearlayout_pinglun.setVisibility(View.GONE);
            hideKeyboard();

        } else if (v.getId() == R.id.send_pinglun_contant) {
            if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
//            if (sp.getBoolean("xianji_denglu", false)) {

                if (editText_pinglun_contant.getText().toString().length() > 0) {

                    pinglun_data();
                } else {
                    Toast.makeText(Main_FuWu_DetailsActivity.this, "请输入评论内容", Toast.LENGTH_SHORT)
                            .show();
                }
            } else {
                Toast.makeText(Main_FuWu_DetailsActivity.this, "请先登录", Toast.LENGTH_SHORT)
                        .show();
            }
        } else if (v.getId() == R.id.iamge_collection) {
            if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
//            if (sp.getBoolean("xianji_denglu", false)) {
                collection();
            } else {
                Toast.makeText(Main_FuWu_DetailsActivity.this, "请先登录", Toast.LENGTH_SHORT)
                        .show();
            }
        } else if (v.getId() == R.id.fuwu_imageview_share) {
            Photo_show();
            province_gridview.setAdapter(mShare_Adapter);
            show_popupWindow.showAtLocation(findViewById(R.id.fuwu_detail),
                    Gravity.BOTTOM, 0, 0);

            province_gridview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                                        int arg2, long arg3) {
                    if (SharesUtil.getShareList().get(arg2).getName().equals("微信好友")) {
                        SharesUtil.weiXin_showShare(share_url, fuwu_details_title.getText().toString());
                    } else if (SharesUtil.getShareList().get(arg2).getName().equals("微信朋友圈")) {
                        SharesUtil.weiXin_prnyyouquan_showShare(share_url, fuwu_details_title.getText().toString());
                    } else if (SharesUtil.getShareList().get(arg2).getName().equals("微信收藏")) {
                        SharesUtil.weiXin_collection_showShare(share_url, fuwu_details_title.getText().toString());
                    } else if (SharesUtil.getShareList().get(arg2).getName().equals("腾讯微博")) {
                        tengxun_weibo_share();
                    } else if (SharesUtil.getShareList().get(arg2).getName().equals("新浪微博")) {
                        WeiBo_share();
                    } else if (SharesUtil.getShareList().get(arg2).getName().equals("QQ")) {
                        QQ_Share();
                    } else if (SharesUtil.getShareList().get(arg2).getName().equals("QQ空间")) {
                        share_QQ_Qzon();
                    }
                }
            });
        }
    }

    /**
     * 隐藏软键盘
     */
    private void hideKeyboard() {
        if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getCurrentFocus() != null)
                manager.hideSoftInputFromWindow(getCurrentFocus()
                        .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    private void showKeyboard() {
        if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getCurrentFocus() != null)
                manager.showSoftInputFromInputMethod(getCurrentFocus()
                        .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 收藏
     */
    public void collection() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("id",
                getIntent().getIntExtra("fuwu_details_id", 0) + "");
        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
//        parms.addBodyParameter("token", sp.getString("xianji_token", ""));
        parms.addBodyParameter("title", fuwu_details_title.getText().toString());
        parms.addBodyParameter("type", "detail");
        httpUtils.send(HttpMethod.POST, Contant.collection, parms,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        try {
                            JSONObject object = new JSONObject(arg0.result);
                            Toast.makeText(Main_FuWu_DetailsActivity.this,
                                    object.getString("msg"), Toast.LENGTH_SHORT).show();

                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                    }
                });
    }

    /**
     * 获取服务详情
     */
    public void get_fuwu_datils() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
//        parms.addBodyParameter("token", sp.getString("xianji_token", ""));
        parms.addBodyParameter("uid", String.valueOf(PreferenceEngine.getInstance().getInt(Constant.SP.NAME, Constant.SP.KEY_UID, 0)));
//        parms.addBodyParameter("uid", sp.getInt("uid", 0) + "");
        parms.addBodyParameter("service_id", getIntent().getIntExtra("fuwu_details_id", 0) + "");
        httpUtils.send(HttpMethod.POST, Contant.fuwu_details, parms,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        fuwu_detail_animation.stop();
                        fuwu_detail_image_load.setVisibility(View.GONE);
                        Toast.makeText(Main_FuWu_DetailsActivity.this, "加载失败",
                                Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        try {
                            JSONObject response = new JSONObject(arg0.result);
                            if (response.getInt("status") == 1) {
                                Log.i("vvvvvv", response + "");
                                final JSONArray array = response
                                        .getJSONArray("data");
                                final JSONObject obj = array.getJSONObject(0);
                                final JSONObject obj1 = array.getJSONObject(1);
                                booth_id = obj.getInt("booth_id");
                                fuwu_id = obj.getInt("id");
                                user_id = obj.getJSONObject("usrinfo").getInt(
                                        "id");
                                phone = obj.getJSONObject("usrinfo").getString(
                                        "mobile");
                                Main_FuWu_DetailsActivity.this.runOnUiThread(new Runnable() {

                                    @Override
                                    public void run() {
                                        try {
                                            fuwu_detail_animation.stop();
                                            fuwu_detail_image_load
                                                    .setVisibility(View.GONE);
                                            fuwu_details_PullToRefreshLayout_two
                                                    .setVisibility(View.VISIBLE);
                                            if (fuwu_details_webview == null) {
                                                fuwu_details_webview = (X5WebView) findViewById(R.id.fuwu_details_webview);
                                            }
                                            fuwu_details_webview
                                                    .loadDataWithBaseURL(
                                                            null,
                                                            obj.getString("details"),
                                                            "text/html",
                                                            "utf-8", null);
                                            fuwu_details_webview.setWebViewClient(new com.example.xianji.utils.MyWebViewClient(fuwu_details_webview));
                                            fuwu_details_webview.addJavascriptInterface(new com.example.xianji.utils.Javascript(
                                                            Main_FuWu_DetailsActivity.this),
                                                    "myObj");

                                            fuwu_details_title.setText(obj
                                                    .getString("title"));
                                            fuwu_details_gongsi_name.setText("公司名称:   "
                                                    + obj.getString("btitle"));
                                            fuwu_details_tag.setText(new StringBuffer(
                                                    "服务分类:   ")
                                                    .append(obj
                                                            .getString("parent_name"))
                                                    .toString());

                                            if (obj.getJSONObject("usrinfo")
                                                    .getString("type")
                                                    .equals("FC")
                                                    || obj.getJSONObject(
                                                    "usrinfo")
                                                    .getString("type")
                                                    .equals("AC")) {
                                                // 企业
                                                button_pinglun
                                                        .setVisibility(View.VISIBLE);
                                            } else {
                                                // 个人
                                                button_pinglun
                                                        .setVisibility(View.GONE);
                                            }

                                            if (obj.getJSONObject("usrinfo")
                                                    .getInt("isopen") == 0) {
                                                fuwu_details_gongsi_phone
                                                        .setText("联系方式 : 点击获取");

                                                fuwu_details_gongsi_phone
                                                        .setOnClickListener(new OnClickListener() {

                                                            @Override
                                                            public void onClick(
                                                                    View v) {
                                                                // Auto-generated
                                                                // method stub
                                                                XJApplication.cachThreadPool
                                                                        .execute(new Runnable() {

                                                                            @Override
                                                                            public void run() {
                                                                                get_phone_number();
                                                                            }
                                                                        });
                                                            }
                                                        });

                                            } else {
                                                fuwu_details_gongsi_phone
                                                        .setText("联系方式 :"
                                                                + obj.getJSONObject(
                                                                "usrinfo")
                                                                .getString(
                                                                        "mobile"));
                                            }
                                            if (obj.getString("authen").equals(
                                                    "D")) {
                                                fuwu_details_is_renzheng
                                                        .setText(new StringBuffer(
                                                                "认证状态:   ")
                                                                .append("未认证")
                                                                .toString());
                                            } else if (obj.getString("authen")
                                                    .equals("A")) {

                                                fuwu_details_is_renzheng
                                                        .setText(new StringBuffer(
                                                                "认证状态:   ")
                                                                .append("申请中")
                                                                .toString());
                                            } else if (obj.getString("authen")
                                                    .equals("N")) {
                                                fuwu_details_is_renzheng
                                                        .setText(new StringBuffer(
                                                                "认证状态:   ")
                                                                .append("未认证")
                                                                .toString());

                                            } else if (obj.getString("authen")
                                                    .equals("Y")) {
                                                fuwu_details_is_renzheng
                                                        .setText(new StringBuffer(
                                                                "认证状态:   ")
                                                                .append("认证通过")
                                                                .toString());

                                            }

                                            if (obj1.getInt("count") > 0) {

                                                replay_list.clear();
                                                JSONArray array2 = array
                                                        .getJSONArray(2);
                                                Log.i("vvbbbbb", array2 + "");
                                                for (int i = 0; i < array2
                                                        .length(); i++) {

                                                    JSONObject object2 = array2
                                                            .getJSONObject(i);

                                                    Replay mReplay = new Replay();

                                                    mReplay.setNickname(object2
                                                            .getJSONObject(
                                                                    "authors")
                                                            .getString(
                                                                    "nickname"));
                                                    mReplay.setUpdated_at(object2
                                                            .getString("updated_at"));
                                                    mReplay.setDetail(object2
                                                            .getString("detail"));
                                                    mReplay.setAvatar(object2
                                                            .getJSONObject(
                                                                    "authors")
                                                            .getString("avatar"));

                                                    replay_list.add(mReplay);
                                                }

                                            }
                                            handler.sendEmptyMessage(2);
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                            }
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                    }
                });
    }

    /**
     * 评论数据请求
     */
    public void pinglun_data() {

        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
//        parms.addBodyParameter("token", sp.getString("xianji_token", ""));
        parms.addBodyParameter("case_id", fuwu_id + "");
        parms.addBodyParameter("detail", editText_pinglun_contant.getText()
                .toString());

        httpUtils.send(HttpMethod.POST, Contant.fuwu_pingjia, parms,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        try {
                            JSONObject object = new JSONObject(arg0.result);
                            Toast.makeText(Main_FuWu_DetailsActivity.this,
                                    object.getString("msg"), Toast.LENGTH_SHORT).show();
                            if (object.getInt("status") == 1) {
                                editText_pinglun_contant.setText("");
                                hideKeyboard();
                                handler.sendEmptyMessage(5);
                            }

                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                    }
                });
    }

    /**
     * 服务评价页面更多
     */

    public void pingjia_more() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("service_id", fuwu_id + "");
        parms.addBodyParameter("page", page + "");

        httpUtils.send(HttpMethod.POST, Contant.pingjia_more, parms,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        fuwu_details_PullToRefreshLayout_two
                                .loadmoreFinish(PullToRefreshLayout_two.SUCCEED);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        try {
                            JSONObject object = new JSONObject(arg0.result);
                            Toast.makeText(Main_FuWu_DetailsActivity.this,
                                    object.getString("msg"), Toast.LENGTH_SHORT).show();
                            if (object.getInt("status") == 1) {
                                JSONArray array = object.getJSONArray("data");
                                if (array.length() > 0) {
                                    for (int i = 0; i < array.length(); i++) {
                                        JSONObject object2 = array
                                                .getJSONObject(i);

                                        Replay mReplay = new Replay();

                                        mReplay.setNickname(object2
                                                .getJSONObject("authors")
                                                .getString("nickname"));
                                        mReplay.setUpdated_at(object2
                                                .getString("updated_at"));
                                        mReplay.setDetail(object2
                                                .getString("detail"));
                                        mReplay.setAvatar(object2
                                                .getJSONObject("authors")
                                                .getString("avatar"));

                                        replay_list.add(mReplay);
                                    }

                                } else {
                                    Toast.makeText(Main_FuWu_DetailsActivity.this,
                                            "没有更多数据", Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                Toast.makeText(Main_FuWu_DetailsActivity.this,
                                        object.getString("msg"), Toast.LENGTH_SHORT).show();
                            }
                            handler.sendEmptyMessage(3);

                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public void onRefresh(PullToRefreshLayout_two pullToRefreshLayout) {
        get_fuwu_datils();
    }

    @Override
    public void onLoadMore(PullToRefreshLayout_two pullToRefreshLayout) {
        page++;
        pingjia_more();
    }

    /**
     * 获取手机号
     */
    public void get_phone_number() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
//        parms.addBodyParameter("token", sp.getString("xianji_token", ""));
        parms.addBodyParameter("uid", user_id + "");

        httpUtils.send(HttpMethod.POST, Contant.huoqu_phone, parms,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        try {
                            JSONObject object = new JSONObject(arg0.result);
                            Toast.makeText(Main_FuWu_DetailsActivity.this,
                                    object.getString("msg"), Toast.LENGTH_SHORT).show();
                            if (object.getInt("status") == 1) {
                                handler.sendEmptyMessage(4);
                            }

                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                    }
                });
    }

    private void Photo_show() {
        layout2 = (LinearLayout) LayoutInflater.from(
                Main_FuWu_DetailsActivity.this).inflate(
                R.layout.province_popoupwindow_listview, null);
        province_gridview = (GridView) layout2
                .findViewById(R.id.province_gridview);
        if (mShare_Adapter == null) {
            mShare_Adapter = new Share_Adapter(this, SharesUtil.getShareList());
        }
        province_gridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
        if (show_popupWindow == null) {
            show_popupWindow = new PopupWindow(layout2,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT);
        }
        show_popupWindow.setBackgroundDrawable(new BitmapDrawable());

        show_popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        show_popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
        show_popupWindow.update();
        show_popupWindow.setOutsideTouchable(true);
        show_popupWindow.setFocusable(true);
        show_popupWindow.setContentView(layout2);
    }

    /**
     * @return
     */
    public void QQ_Share() {
        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,
                QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, "贤集网");
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, fuwu_details_title
                .getText().toString());
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, share_url);
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "贤集网");
        XJApplication.getTencent().shareToQQ(Main_FuWu_DetailsActivity.this,
                params, new Share_Listener());
    }

    public void share_QQ_Qzon() {
        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,
                QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, "贤集网");
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, fuwu_details_title
                .getText().toString());
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, share_url);
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "贤集网");
        XJApplication.getTencent().shareToQQ(Main_FuWu_DetailsActivity.this,
                params, new Share_Listener());
    }

    private void tengxun_weibo_share() {
        XJApplication.getTencent().login(Main_FuWu_DetailsActivity.this, "all",
                loginListener);
    }

    /**
     * 腾讯微博
     */

    public void initOpenidAndToken(JSONObject jsonObject) {
        try {
            String token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN);
            String expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN);
            String openId = jsonObject.getString(Constants.PARAM_OPEN_ID);
            if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(expires)
                    && !TextUtils.isEmpty(openId)) {
                XJApplication.getTencent().setAccessToken(token, expires);
                XJApplication.getTencent().setOpenId(openId);
            }
        } catch (Exception e) {
        }
    }

    /**
     * 腾讯微博是否登录
     *
     * @param context
     * @return
     */
    public boolean ready(Context context) {
        if (XJApplication.getTencent() == null) {
            return false;
        }
        boolean ready = XJApplication.getTencent().isSessionValid()
                && XJApplication.getTencent().getQQToken().getOpenId() != null;
        if (!ready) {
            Toast.makeText(context, "login and get openId first, please!",
                    Toast.LENGTH_SHORT).show();
        }
        return ready;
    }

    private void WeiBo_share() {
        mSsoHandler = new SsoHandler(Main_FuWu_DetailsActivity.this,
                XJApplication.mAuthInfo);

        mSsoHandler.authorizeClientSso(new AuthListener());

        mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(
                Main_FuWu_DetailsActivity.this, "4216978203");

        mWeiboShareAPI.registerApp();

        isInstalledWeibo = mWeiboShareAPI.isWeiboAppInstalled();

        supportApiLevel = mWeiboShareAPI.getWeiboAppSupportAPI();

        mWeiboShareAPI.handleWeiboResponse(getIntent(),
                Main_FuWu_DetailsActivity.this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        mWeiboShareAPI.handleWeiboResponse(intent, this);
    }

    @Override
    public void onResponse(BaseResponse baseResp) {
        if (baseResp != null) {
            switch (baseResp.errCode) {
                case WBConstants.ErrorCode.ERR_OK:
                    Toast.makeText(this, "成功", Toast.LENGTH_LONG).show();
                    break;
                case WBConstants.ErrorCode.ERR_CANCEL:
                    Toast.makeText(this, "取消", Toast.LENGTH_LONG).show();
                    break;
                case WBConstants.ErrorCode.ERR_FAIL:
                    Toast.makeText(this, "Error Message: " + baseResp.errMsg,
                            Toast.LENGTH_LONG).show();
                    break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // SSO 授权回调
        // 重要：发起 SSO 登陆的 Activity 必须重写 onActivityResults
        if (mSsoHandler != null) {
            mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
        }

        /****************************** QQ 分享 必须加的，回调不起作用 ********************************/
        // 官方文档没这句代码, 但是很很很重要, 不然不会回调!
        Tencent.onActivityResultData(requestCode, resultCode, data,
                new Share_Listener());

        if (requestCode == Constants.REQUEST_API) {
            if (resultCode == Constants.REQUEST_QQ_SHARE
                    || resultCode == Constants.REQUEST_QZONE_SHARE
                    || resultCode == Constants.REQUEST_OLD_SHARE) {
                Tencent.handleResultData(data, new Share_Listener());
            }
        }
    }

    private BaseMediaObject getWebpageObj() {
        WebpageObject mediaObject = new WebpageObject();
        mediaObject.identify = Utility.generateGUID();
        mediaObject.title = fuwu_details_title.getText().toString();
        mediaObject.description = "贤集网" + share_url;

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.touxiang);
        mediaObject.setThumbImage(bitmap);
        mediaObject.actionUrl = share_url;
        mediaObject.defaultText = "";
        return mediaObject;
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis())
                : type + System.currentTimeMillis();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        handler.removeCallbacksAndMessages(null);
        if (fuwu_details_webview != null) {
            fuwu_details_webview.destroy();
            fuwu_details_webview = null;
        }
    }

    /******************* qq分享的回调 *********************************/
    public class Share_Listener implements IUiListener {
        @Override
        public void onCancel() {
            Toast.makeText(Main_FuWu_DetailsActivity.this, "取消", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onComplete(Object arg0) {
            Toast.makeText(Main_FuWu_DetailsActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(UiError arg0) {
            Toast.makeText(Main_FuWu_DetailsActivity.this, arg0 + "", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 腾讯微博
     */
    private class BaseUiListener implements IUiListener {
        @Override
        public void onComplete(Object response) {
            if (null == response) {
                Toast.makeText(Main_FuWu_DetailsActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                return;
            }
            JSONObject jsonResponse = (JSONObject) response;
            if (null != jsonResponse && jsonResponse.length() == 0) {
                Toast.makeText(Main_FuWu_DetailsActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                return;
            }
            // Toast.makeText(Main_FuWu_DetailsActivity.this,"登录成功", 0).show();
            doComplete((JSONObject) response);
        }

        protected void doComplete(JSONObject values) {
        }

        @Override
        public void onError(UiError e) {
            Toast.makeText(Main_FuWu_DetailsActivity.this, e.errorDetail, Toast.LENGTH_SHORT)
                    .show();
        }

        @Override
        public void onCancel() {
            Toast.makeText(Main_FuWu_DetailsActivity.this, "oncancle", Toast.LENGTH_SHORT)
                    .show();
        }
    }

    /**
     * 腾讯微博
     *
     * @author me
     */
    private class TQQApiListener extends BaseUiListener {
        private String mScope = "all";
        private Boolean mNeedReAuth = false;
        private Activity mActivity;

        public TQQApiListener(String mScope, Boolean mNeedReAuth,
                              Activity mActivity) {
            super();
            this.mScope = mScope;
            this.mNeedReAuth = mNeedReAuth;
            this.mActivity = mActivity;
        }

        @Override
        public void onComplete(Object response) {
            final Activity activity = Main_FuWu_DetailsActivity.this;
            try {
                JSONObject json = (JSONObject) response;
                int ret = json.getInt("ret");
                if (ret == 0) {
                    Toast.makeText(Main_FuWu_DetailsActivity.this, "微博发送成功", Toast.LENGTH_SHORT)
                            .show();
                    if (json.has("data")) {
                        JSONObject data = json.getJSONObject("data");
                        if (data.has("id")) {
                            mLastAddTweetId = data.getString("id");
                        }
                    }
                } else if (ret == 100030) {
                    if (mNeedReAuth) {
                        Runnable r = new Runnable() {
                            public void run() {
                                XJApplication.getTencent().reAuth(activity,
                                        mScope, new BaseUiListener());
                            }
                        };
                        Main_FuWu_DetailsActivity.this.runOnUiThread(r);
                    }
                } else if (ret == 6) {
                    Toast.makeText(Main_FuWu_DetailsActivity.this, "该账号未开通微博",
                            Toast.LENGTH_LONG).show();
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    class AuthListener implements WeiboAuthListener {

        @Override
        public void onComplete(Bundle values) {
            mAccessToken = Oauth2AccessToken.parseAccessToken(values);
            String phoneNum = mAccessToken.getPhoneNum();
            if (mAccessToken.isSessionValid()) {
                if (isInstalledWeibo) {
                    if (mWeiboShareAPI.isWeiboAppSupportAPI()) {
                        if (supportApiLevel >= 10351 /*
                         * ApiUtils.BUILD_INT_VER_2_2
                         */) {
                            WeiboMultiMessage weiboMessage = new WeiboMultiMessage();
                            weiboMessage.mediaObject = getWebpageObj();
                            SendMultiMessageToWeiboRequest request = new SendMultiMessageToWeiboRequest();
                            request.transaction = String.valueOf(System
                                    .currentTimeMillis());
                            request.multiMessage = weiboMessage;
                            mWeiboShareAPI.sendRequest(
                                    Main_FuWu_DetailsActivity.this, request);
                        } else {
                            WeiboMessage weiboMessage = new WeiboMessage();
                            weiboMessage.mediaObject = getWebpageObj();
                            SendMessageToWeiboRequest request = new SendMessageToWeiboRequest();
                            request.transaction = String.valueOf(System
                                    .currentTimeMillis());
                            request.message = weiboMessage;
                            mWeiboShareAPI.sendRequest(
                                    Main_FuWu_DetailsActivity.this, request);
                        }
                    }
                }
                Toast.makeText(Main_FuWu_DetailsActivity.this, "授权成功",
                        Toast.LENGTH_SHORT).show();
            } else {
                String code = values.getString("code");
                /*
                 * if (!TextUtils.isEmpty(code)) { message = message +
                 * "\nObtained the code: " + code; }
                 */
                Toast.makeText(Main_FuWu_DetailsActivity.this, "授权失败",
                        Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onCancel() {
            Toast.makeText(Main_FuWu_DetailsActivity.this, "取消",
                    Toast.LENGTH_LONG).show();
        }

        @Override
        public void onWeiboException(WeiboException e) {
            Toast.makeText(Main_FuWu_DetailsActivity.this,
                    "Auth exception : " + e.getMessage(), Toast.LENGTH_LONG)
                    .show();
        }
    }

}
